<?php
session_start();
if (!$_SESSION['logged_in']) {
    header('Location: login.php');
    exit;
}

if (isset($_GET['file'])) {
    $file = urldecode($_GET['file']);
    $content = file_get_contents('articles/' . $file);
} else {
    header('Location: admin.php');
    exit;
}

if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $content = $_POST['content'];
    file_put_contents('articles/' . $file, $content);
    header('Location: admin.php');
    exit;
}
?>

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>编辑文章</title>
    <link rel="stylesheet" href="assets/css/style.css">
    <script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
    <link href="https://cdn.jsdelivr.net/npm/summernote@0.8.20/dist/summernote-lite.min.css" rel="stylesheet">
    <script src="https://cdn.jsdelivr.net/npm/summernote@0.8.20/dist/summernote-lite.min.js"></script>
    <script src="https://cdn.bootcss.com/summernote/0.8.20/lang/summernote-zh-CN.js"></script>
</head>
<body>
    <header>
        <h1>编辑文章</h1>
        <a href="admin.php" class="button">返回后台</a>
    </header>
    <main>
        <form method="post">
            <textarea id="editor" name="content"><?php echo htmlspecialchars($content); ?></textarea>
            <script>
                $(document).ready(function() {
                    $('#editor').summernote({
                        placeholder: '...',
                        tabsize: 2,
                        lang: 'zh-CN',
                        height: 300,
                        toolbar: [
                            ['style', ['style']],
                            ['font', ['bold', 'italic', 'underline', 'clear', 'strikethrough', 'subscript', 'superscript']],
                            ['fontsize', ['fontsize']],
                            ['fontname', ['fontname']],
                            ['color', ['color']],
                            ['para', ['ul', 'ol', 'paragraph']],
                            ['height', ['height']],
                            ['insert', ['table', 'link', 'picture', 'video', 'hr']],
                            ['view', ['fullscreen', 'codeview', 'help', 'preview']],
                        ],
                        callbacks: {
                            onImageUpload: function(files, editor, welEditable) {
                                sendFile(files[0], editor, welEditable);
                            }
                        }
                    });
                });

                function sendFile(file, editor, welEditable) {
                    var data = new FormData();
                    data.append("file", file);
                    $.ajax({
                        data: data,
                        type: "POST",
                        url: "upload.php",
                        cache: false,
                        contentType: false,
                        processData: false,
                        success: function(url) {
                            editor.insertImage(welEditable, url, function($image) {
                                $image.css('width', '100%'); // 设置图片宽度为 100%
                                $image.css('height', 'auto'); // 设置图片高度为自动，保持比例
                            });
                        }
                    });
                }
            </script>
            <input type="submit" value="保存">
        </form>
    </main>
</body>
</html>